/**
 * 
 */
package com.ctrip.cap.device.ios.impl;

import java.util.Set;

import com.ctrip.cap.device.AbstractDeviceStore;
import com.ctrip.cap.device.DeviceStore;
import com.ctrip.cap.device.exceptions.DeviceNotFoundException;
import com.ctrip.cap.device.exceptions.IOSDeviceException;
import com.ctrip.cap.device.ios.IOSDevice;

/**
 * @author zwxu
 *
 */
public class IPhoneDeviceStore extends AbstractDeviceStore<IOSDevice> implements
		DeviceStore<IOSDevice> {

	IPhoneDeviceManager deviceManager = new IPhoneDeviceManager();

	static class StoreHolder {

		static final IPhoneDeviceStore instance = init();

		static IPhoneDeviceStore init() {
			IPhoneDeviceStore instance;
			instance = new IPhoneDeviceStore();
			instance.initIOSDevices();
			return instance;
		}
	}

	public static IPhoneDeviceStore getInstance() {
		return StoreHolder.instance;
	}

	/**
	 * call once
	 * 
	 * @param shouldKeepAdbAlive
	 */
	public void initIOSDevices() throws IOSDeviceException {

		deviceManager.initializeConnection();
		Set<IOSDevice> iosDevices = deviceManager.getIOSDevices();

		if (iosDevices.isEmpty()) {
			throw new DeviceNotFoundException(
					"No IOS devices were found. Please make sure your device are connected.");
		}

		devices.addAll(iosDevices);
		devicesInUse.addAll(iosDevices);
	}

	@Override
	public void shutdown() {
		deviceManager.shutdown();
	}

	@Override
	public void shutdownForcely() {
		deviceManager.shutdownForcely();

	}
}